/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewFeedbackModule.java
 *
 * Created on Dec 2, 2009, 3:37:38 PM
 */

package GUI;
import java.util.Vector;
import Bussiness.B_Search;
import DatabaseAccess.D_DBHelper;
import java.util.logging.Level;
import java.util.logging.Logger;
import Bussiness.B_TableDataLoad;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.JTextComponent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import Bussiness.MJComboBoxKeyListener;
/**
 *
 * @author TienDung
 */
public class NewFeedbackModule extends javax.swing.JPanel {
    private int cusID=0;
    /** Creates new form NewFeedbackModule */
    B_TableDataLoad tabledata = null;
    Vector vector = null;
    public JTextComponent editor=null;
    public B_Search bSearch=null;
    private int totalRecord=0;
    private int totalPage;
    private int pageSize=100;
    private static int next=1;
    public  Connection con;
    public Statement st;
    public NewFeedbackModule(int CusID) {
        this.cusID = CusID;
        initComponents();
        //rbProductName.setSelected(true);
       // vector = new Vector();
        //vector.add(cusID);
        //tabledata = new B_TableDataLoad("connection.cfg");
       // ProductListTable.setModel(tabledata.setTableModelParam(ProductListTable, "GetProductOfCustomer", vector));
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:myDB");
            st=con.createStatement();
        }catch(Exception e){
            e.printStackTrace();
        }
        serachProduct();
        //pagePosition();
    }

     public void serachProduct(){
        editor = (JTextComponent)boxSearch.getEditor().getEditorComponent();
        boxSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editor.setSelectionStart(editor.getText().length());
                editor.setSelectionEnd(editor.getText().length());
            }
        });
        MJComboBoxKeyListener key=new MJComboBoxKeyListener(boxSearch);
        key.setInformation("FindProduct",this.cusID,0,2);
        editor.addKeyListener(key);
         /*int type=0;
        if(rbDispatchNo.isSelected()){
            type=1;
        }
        editor = (JTextComponent)boxSearch.getEditor().getEditorComponent();
        bSearch=new B_Search(boxSearch);
        if(bSearch.setInformation("FindProduct",this.cusID,type,2))
        {}
        boxSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editor.setSelectionStart(editor.getText().length());
                editor.setSelectionEnd(editor.getText().length());
            }
        });
        editor.addKeyListener(bSearch);
       //}*/
    }

     public void pagePosition(){
      try{
       tabledata.crs.last();
       totalRecord=tabledata.crs.getRow();
       System.out.println("Total record : "+tabledata.crs.getRow());
    
       if(totalRecord % pageSize==0)
           totalPage=totalRecord/pageSize;
       else
          totalPage=(totalRecord/pageSize)+1;

       tabledata.crs.setMaxRows(totalRecord);
       tabledata.crs.setPageSize(totalPage);
       tabledata.crs.nextPage();
       txtTotalPage.setText("Total page : "+totalPage);
       txtPage.setText(""+next);
      }catch(Exception e){
        e.printStackTrace();
      }
     }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnSearch = new javax.swing.JButton();
        rbDispatchNo = new javax.swing.JRadioButton();
        rbProductName = new javax.swing.JRadioButton();
        boxSearch = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ProductListTable = new javax.swing.JTable();
        txtTotalPage = new javax.swing.JLabel();
        btnPrevious = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        txtPage = new javax.swing.JTextField();
        btnGo = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        btnViewList = new javax.swing.JButton();
        filterOrderDate = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Search by dispatchNo or product name"));

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/find8.png"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        rbDispatchNo.setText("DipatchNo");
        rbDispatchNo.setMultiClickThreshhold(1L);

        rbProductName.setText("Product name");

        boxSearch.setEditable(true);
        boxSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rbDispatchNo)
                        .addGap(18, 18, 18)
                        .addComponent(rbProductName, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(boxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(60, 60, 60))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbProductName)
                    .addComponent(rbDispatchNo)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Product list"));

        ProductListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Dispatch no", "Product Name", "Date Order", "Warranty"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        ProductListTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ProductListTableMousePressed(evt);
            }
        });
        jScrollPane2.setViewportView(ProductListTable);

        txtTotalPage.setText("Total page :");

        btnPrevious.setText("<-");
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });

        btnNext.setText("->");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnGo.setText("Go");
        btnGo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtTotalPage)
                        .addGap(38, 38, 38)
                        .addComponent(btnPrevious, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnNext)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtPage, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnGo))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 807, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTotalPage)
                    .addComponent(btnPrevious)
                    .addComponent(btnNext)
                    .addComponent(txtPage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/icon_newFeedback.png"))); // NOI18N
        jButton1.setText("Add New Feedback");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnViewList.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnViewList.setText("Viewlist");

        filterOrderDate.setBorder(javax.swing.BorderFactory.createTitledBorder("Filter order by date"));

        jLabel1.setText("Order date");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select date", " " }));

        javax.swing.GroupLayout filterOrderDateLayout = new javax.swing.GroupLayout(filterOrderDate);
        filterOrderDate.setLayout(filterOrderDateLayout);
        filterOrderDateLayout.setHorizontalGroup(
            filterOrderDateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filterOrderDateLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBox1, 0, 212, Short.MAX_VALUE)
                .addContainerGap())
        );
        filterOrderDateLayout.setVerticalGroup(
            filterOrderDateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filterOrderDateLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(filterOrderDateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addGap(18, 18, 18)
                            .addComponent(btnViewList, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(84, 84, 84))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(filterOrderDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addContainerGap()))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(filterOrderDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(btnViewList, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ProductListTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ProductListTableMousePressed
        // TODO add your handling code here:
        ProductListTable.removeEditor();
        if(evt.getClickCount()>1){
            int iRow=this.ProductListTable.getSelectedRow();
            String s1=((String)this.ProductListTable.getValueAt(iRow , 0));
            String s2= (String)this.ProductListTable.getValueAt(iRow , 1);
            String s3=  (String)this.ProductListTable.getValueAt(iRow , 2);
            String s4 = ((String)this.ProductListTable.getValueAt(iRow , 3));
            //String s5 = "sao laij the";
            AddNewFeedback addnew = new AddNewFeedback(frmParent, true);
            
            addnew.setForm(cusID,s1, s2, s3, s4);
            addnew.setVisible(true);
        }
    }//GEN-LAST:event_ProductListTableMousePressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        ProductListTable.removeEditor();
        if(ProductListTable.getSelectedRowCount()==1){
            int iRow=this.ProductListTable.getSelectedRow();
            String s1=((String)this.ProductListTable.getValueAt(iRow , 0));
            String s2= (String)this.ProductListTable.getValueAt(iRow , 1);
            String s3=  (String)this.ProductListTable.getValueAt(iRow , 2);
            String s4 = ((String)this.ProductListTable.getValueAt(iRow , 3));
            //String s5 = "sao laij the";
            AddNewFeedback addnew = new AddNewFeedback(frmParent, true);
           
            addnew.setForm(cusID,s1, s2, s3, s4);
            addnew.setVisible(true);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviousActionPerformed
        // TODO add your handling code here:
        if(totalPage>1){
            try{
              btnNext.setEnabled(true);
              next--;
              txtPage.setText(""+next);
              tabledata.crs.previousPage();
              ProductListTable.setModel(tabledata.setTableModel(ProductListTable));
              if(next==1)
                    btnPrevious.setEnabled(false);
        }catch(Exception e){
            e.printStackTrace();
        }
        }
}//GEN-LAST:event_btnPreviousActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        if(totalPage>1){
         try{
            btnPrevious.setEnabled(true);
            next++;
            txtPage.setText(""+next);
            if(next==totalPage)
                btnNext.setEnabled(false);
            tabledata.crs.nextPage();
            ProductListTable.setModel(tabledata.setTableModel(ProductListTable));
        }catch(Exception e){
            e.printStackTrace();
        }
       }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnGoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnGoActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
           int type=0;
           Vector vect=new Vector();
           vect.add(this.cusID);
           vect.add((String)boxSearch.getSelectedItem());
           if(rbDispatchNo.isSelected())
               type=1;
           vect.add(type);
           ProductListTable.setModel(tabledata.setTableModelParam(ProductListTable, "FindProduct",vect));
    }//GEN-LAST:event_btnSearchActionPerformed

    private JFrame frmParent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable ProductListTable;
    private javax.swing.JComboBox boxSearch;
    private javax.swing.JButton btnGo;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnViewList;
    private javax.swing.JPanel filterOrderDate;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JRadioButton rbDispatchNo;
    private javax.swing.JRadioButton rbProductName;
    private javax.swing.JTextField txtPage;
    private javax.swing.JLabel txtTotalPage;
    // End of variables declaration//GEN-END:variables

}
